package com.qingke.oms.command.shop;

import java.util.Set;

import com.qingke.oms.command.ShopCommand;
import com.qingke.oms.pojo.GoodsStatistic;
import com.qingke.oms.pojo.ItemQuantity;
import com.qingke.oms.pojo.Shop;
import com.qingke.oms.pojo.ShoppingCart;
import com.qingke.oms.pojo.ShoppingCartItem;
import com.qingke.oms.pojo.User;
import com.qingke.oms.util.OMSConsole;

public class AddToShoppingCartCommand extends ShopCommand {

	public AddToShoppingCartCommand(User user, Shop shop) {
		super(user, shop);
	}

	@Override
	public void execute() {
		int goodsId=OMSConsole.getInt("goods id:");
		shop=dao.selectNewestShopById(shop.getId());
		Set<GoodsStatistic> goodsStatistics=shop.getGoodsStatistics();
		boolean isExist=false;
		GoodsStatistic goodsStatistic=null;
		for(GoodsStatistic gs:goodsStatistics){
			if(gs.getGoods().getId()==goodsId){
				goodsStatistic=gs;
				isExist=true;
				break;
			}
		}
		if(!isExist){
			OMSConsole.println("There is no the id of the goods in the shop.");
			return ;
		}
		
		int quantity=OMSConsole.getInt("quantity:");
		if(quantity==0){
			OMSConsole.println("cancel add goods to shopping cart.");
			return ;
		}
		int stock=goodsStatistic.getStock();
		if(quantity>stock){
			OMSConsole.println("The input is more than the stock of the goods.");
			OMSConsole.println("Default the input equal the stock of the goods.");
			OMSConsole.println("[new input]:"+stock);
			quantity=stock;
		}
		
		ShoppingCart shoppingCart=null;
		if(!dao.isShoppingCartExistByCustomerAndShop(user,shop)){
			shoppingCart=new ShoppingCart();
			shoppingCart.setCustomer(user);
			shoppingCart.setShop(shop);
			boolean isSuccess=dao.insertShoppingCart(shoppingCart);
			if(!isSuccess){
				OMSConsole.println("create your shopping cart failed.");
				return ;
			}
		}
		
		shoppingCart=dao.selectShoppingCartByCustomerAndShop(user,shop);
		boolean isShoppingCartItemExist=dao.isShoppingCartItemExistByShoppingCartAndGoods(shoppingCart, goodsStatistic.getGoods());
		ShoppingCartItem shoppingCartItem=null;
		if(!isShoppingCartItemExist){
			shoppingCartItem=new ShoppingCartItem();
			shoppingCartItem.setShoppingCart(shoppingCart);
			shoppingCartItem.setGoods(goodsStatistic.getGoods());
			shoppingCartItem.setPrice(goodsStatistic.getPrice());
			boolean isSuccess=dao.insertShoppingCartItem(shoppingCartItem);
			if(!isSuccess){
				OMSConsole.println("create shopping cart item failed.");
				return ;
			}
		}
		shoppingCartItem=dao.selectShoppingCartItemByShoppingCartAndGoods(shoppingCart, goodsStatistic.getGoods());
		int oldQuantity=shoppingCartItem.getQuantity();
		
		if(oldQuantity+quantity>goodsStatistic.getStock()){
			OMSConsole.println("add too much");
			return ;
		}
		ItemQuantity itemQuantity=new ItemQuantity();
		itemQuantity.setShoppingCartItem(shoppingCartItem);
		itemQuantity.setQuantity(oldQuantity+quantity);
		boolean isSuccess=dao.insertItemQuantity(itemQuantity);
		if(!isSuccess){
			OMSConsole.println("create item quantity failed.");
			return ;
		}
		dao.clear();
		shoppingCart=dao.selectShoppingCartByCustomerAndShop(user, shop);
		shoppingCart.calculateTotal();
		boolean isSuccessUpdate=dao.updateShoppingCart(shoppingCart);
		if(!isSuccessUpdate){
			OMSConsole.println("update shopping cart total failed.");
			OMSConsole.println("The total need be careful.");
			return ;
		}
		OMSConsole.println("Add success.");
		
	}

}
